#!/bin/sh

if [ ! -s /sbin/cluster/clhaltapp ]
then
	cd /sbin/cluster
	[ ! -e clhaltapp ] && ln -s clstartapp clhaltapp
	[ ! -e cldaemonctl ] && ln -s lemsctl cldaemonctl
	[ ! -e clnetctl ] && ln -s lemsctl clnetctl
	[ ! -e clhbdctl ] && ln -s lemsctl clhbdctl
fi

# If we have a classic /etc/init.d directory then add an
# entry suitable for downing drbd interfaces if a reboot occurs
# to reduce chance of data inconsistencies.

MP=""

if [ -f /etc/man.config ]
then
	MP=/etc/man.config
fi

if [ -f /etc/manpath.config ]
then
	MP=/etc/manpath.config
fi

if [ -n "$MP" ]
then
	add_manpath=1
	if [ -n "$(egrep '^MANDATORY_MANPATH.*/usr/local/cluster/man' $MP)" ]
	then
		add_manpath=0
	fi
	if [ -n "$(egrep '^MANPATH.*/usr/local/cluster/man' $MP)" ]
	then
		add_manpath=0
	fi

	awk '$1 == "SECTION" {
		f=0;
		for(i=2; i<=NF; i++) {
			if($(i) == "1m") {
				f=1; break;
			}
		}
		if(f==0) {
			$0=$0 " 1m";
		}
	}
	$1 == "MANSECT" {
		f=0;
		if(index($2,"1m:") || index($2,":1m:") || index($2,":1m")) {
			f=1;
		}
		if(f==0) {
			$0=sprintf("%s%s\n",$0,":1m");
		}
	}
	{ print $0; }' < $MP > $MP.new
	if [ $add_manpath -eq 1 ]
	then
		if [ "$MP" = /etc/man.config ]
		then
			echo "MANPATH  /usr/local/cluster/man" >>$MP.new
		else
			echo "MANDATORY_MANPATH  /usr/local/cluster/man" >>$MP.new
		fi
	fi
	if ! cmp $MP $MP.new >/dev/null 2>&1
	then
		mv $MP.new $MP
		chmod 644 $MP
	else
		rm -f $MP.new
	fi
fi

if [ -d /etc/init.d ]
then
	echo '
#!/bin/sh

case "$1" in
  stop)
    /sbin/cluster/tools/down_all_drbd 
    ;;
esac

exit 0' >/etc/init.d/linuxha_drbd
	chmod 555 /etc/init.d/linuxha_drbd
	cwd=`pwd`
	if [ -d /etc/rc3.d ]
	then
		cd /etc/rc3.d
		ln -sf ../init.d/linuxha_drbd K13linuxha_drbd
		cd $cwd
	fi
	if [ -d /etc/rc5.d ]
	then
		cd /etc/rc5.d
		ln -sf ../init.d/linuxha_drbd K13linuxha_drbd
		cd $cwd
	fi
fi

if [ ! -d /var/log/cluster ]
then
	mkdir -p /var/log/cluster >&2
fi

if [ ! -d /var/log/cluster/lems ]
then
	mkdir -p /var/log/cluster/lems >&2
fi

r=0
cd /sbin/cluster/tools
make send_arp >/dev/null 2>&1
if [ ! -f send_arp ]
then
	echo "Warning: Unable to build send_arp!" >&2
	echo "         Although no longer used directly it might be called on" >&2
	echo "         if the custom Perl module does not compile, so check" >&2
	echo "         the remaining output for further potential problems." >&2
	send_arp=0
else
	send_arp=1
fi

# If mii-diag does not build it is probably due to 2.6 kernel
# so check for mii-tool and link to that instead.

mii_tools=1
make mii-diag >/dev/null 2>&1
if [ ! -f /sbin/cluster/mii-diag ]
then
	echo "Warning: Unable to build mii-diag!" >&2
	s=""
	[ -x /sbin/mii-tool ] && s=/sbin/mii-tool
	[ -x /usr/sbin/mii-tool ] && s=/usr/sbin/mii-tool
	[ -x /usr/bin/mii-tool ] && s=/usr/bin/mii-tool
	if [ -z "$s" ]
	then
		echo "Warning: Also can not find mii-tool!" >&2
		echo "         Although not a direct problem - these are used" >&2
		echo "         as a fall-back if a custom Perl module can not be" >&2
		echo "         compiled so check the remaining output for further" >&2
		echo "         potential problems." >&2
		mii_tools=0
	else
		echo "Warning: Found $s and will use instead as a fall-back if." >&2
		echo "         custom perl module can not be compiled." >&2
		cp $s /sbin/cluster/mii-diag && chmod +x /sbin/cluster/mii-diag
	fi
fi

dusp=1
make dusp >/dev/null 2>&1
if [ ! -f dusp ]
then
	echo "Error: Unable to build dusp - please see below for potential problems." >&2
	dusp=0
fi

# REMOVAL OF OLD DRBD's (if still present) - all version 0.7

# BUILD DRBD 8.3.2 CONFIGURATION
echo ""
echo "########################################"
echo "#### BUILD DRBD 8.3.2 CONFIGURATION ####"
echo "########################################"
echo ""

modprobe drbd >/dev/null 2>&1
f=0

drbdinstalled=0
if [ -f /proc/drbd ]
then
	v=$(gawk 'NR==1 {print $2}' /proc/drbd)
	rmmod drbd >/dev/null 2>&1 # incase user builds cluster straightaway!
	if [ "$v" = "8.3.2" ]
	then
		echo "Current environment already has 8.3.2 of DRBD"
		echo "so no work needs to be performed for DRBD."
		f=1
	else
		echo "Current version of DRBD found is $v."
		echo "Linuxha.net supports 0.7.21+ but comes with version"
		echo "8.3.2 - so this is the version that will now be"
		echo "installed."
	fi
fi

r=0
if [ $f = 0 ]
then
	cd /usr/src/cluster/drbd-8.3.2/drbd 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo "Unable to change directory to /usr/src/cluster/drbd-8.3.2/drbd"  >&2
		r=1
	fi
	ln -sf drbd/linux/drbd_config.h drbd_config.h

	make clean all
	if [ $? -ne 0 ]
	then
		echo "Unable to build DRBD Kernel module!" >&2
		r=1
	fi

	cd ..
	make tools 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo "Unable to build DRBD User space tools!" >&2
		r=1
	fi

	make install
	if [ $? -ne 0 ]
	then
		echo "Unable to install DRBD!" >&2
		r=1
	else
		echo ""
		echo "###################################"
		echo "Installation of DRBD was successful"
		echo "###################################"
		echo ""
	fi
	drbdinstalled=1
else
	#################################################################
	# Check if the user space tools are present, and if not then	#
	# compile them - useful for distributions like SUSE which 	#
	# include the kernel module, but not the user-space tools.	#
	#################################################################
	if [ ! -f /sbin/drbdsetup ]
	then
		cd /usr/src/cluster/drbd-8.3.2
		ln -sf drbd/linux/drbd_config.h drbd_config.h
		if [ $? -ne 0 ]
		then
			echo "Unable to create drbd_config.h link for DRBD" >&2
			r=1
		else
			make tools
			if [ $? -ne 0 ]
			then
				echo "Unable to build DRBD User space tools!" >&2
				r=1
			fi

			make install
			if [ $? -ne 0 ]
			then
				echo "Unable to install DRBD User space tools!" >&2
				r=1
			else
				echo ""
				echo "####################################################"
				echo "Installation of DRBD User Space tools was successful"
				echo "####################################################"
				echo ""
			fi
			drbdinstalled=2
		fi
	else
		drbdinstalled=3
	fi
fi

if [ $r = 0 ]
then
	drbd=1
else
	drbd=0
fi

# If the DRBD init stuff is in place, then remove it.
if [ -f /etc/rc.d/init.d/drbd ]
then
	rm -f /etc/rc.d/init.d/drbd
fi

#########################################################################
# If the module is not in the correct place seach for it and then	#
# move it and run depmod -a again...					#
#########################################################################

if [ $drbdinstalled -gt 0 ]
then
	path=/lib/modules/`uname -r`/kernel/drivers/block
	ty=`uname -r`
	ty=`echo $ty|awk '{print substr($0,1,3)}'`
	search=""
	if [ "$ty" = "2.4" ]
	then
		if [ ! -f $path/drbd.o ]
		then
			echo "Warning: Unable to find drbd.o in expected location." >&2
			search=drbd.o
		fi
	elif [ "$ty" = "2.6" ]
	then
		if [ ! -f $path/drbd.ko ]
		then
			echo "Warning: Unable to find drbd.ko in expected location." >&2
			search=drbd.ko
		fi

	else
		echo "Error: Unknown kernel revision - $ty!" >&2
		exit 1
	fi

	
	# If search is set then look for the named file under /lib/modules,	#
	# and copy it to correct location if found.

	if [ "$search" != "" ]
	then
		if [ $drbdinstalled -eq 1 ]
		then
			cmod=`find /lib/modules \( -mtime 0 -o -mtime -1 \) -type f -name $search 2>/dev/null`
		else
			cmod=`find /lib/modules -type f -name $search 2>/dev/null`
		fi
		if [ "$cmod" != "" ]
		then
			cp -fp $cmod $path 2>/dev/null
			if [ ! -f "$path/$search" ]
			then
				echo "Error: Unable to copy file:" >&2
				echo "       $cmod -> $path/$search" >&2
				echo "       DRBD installation NOT successful" >&2
				drbdinstalled=0
			else
				echo "Warning: Copied module $search to following path:" >&2
				echo "         $path/$search" >&2
				depmod -a 2>/dev/null
			fi
		else
			echo "Error: Unable to locate $search under /lib/modules -" >&2
			echo "       build of drbd above probably failed." >&2
			echo "" >&2
			drbdinstalled=0
		fi
	fi
fi

# For each 3rd party (and miitoollib) library we only install them if they
# are not currently present.

perls_to_install=`/sbin/cluster/tools/install_test`

[ $? -ne 0 ] && exit 1

# We also need to build the send_arp routine on the remote host
LC_ALL="C"
export LC_ALL
for cline in `/sbin/cluster/tools/install_test`
do
	echo "###############################"
	echo "Attempting to install $cline..."
	echo "###############################"
	if [ ! -d /usr/src/cluster/$cline ]
	then
		echo "Error: Directory /usr/src/cluster/$cline does not exist!" >&2
		rc=1
		continue
	fi
	cd /usr/src/cluster/$cline
	perl Makefile.PL 
	if [ $? -ne 0 ]
	then
		echo "Warning: Error returned from 'perl Makefile.PL' for $cline!" >&2
		rc=1
		continue
	fi
	make
	if [ $? -ne 0 ]
	then
		echo "Warning: Error returned from 'make' for $cline!" >&2
		rc=1
		continue
	fi
	make test
	if [ $? -ne 0 ]
	then
		echo "Warning: Error returned from 'make test' for $cline!" >&2
	fi
	make install
	if [ $? -ne 0 ]
	then
		echo "Warning: Error returned from 'make install' for $cline!" >&2
		rc=1
		continue
	fi
	echo "###############################"
	echo "Installation of $cline was successful"
	echo "###############################"
done

# Copy drbdsetup to our own dir
if [ -x /sbin/cluster/useful/copy_drbd_bins ]
then
	/sbin/cluster/useful/copy_drbd_bins
fi

r=0
echo ""
echo "Installation Summary"
echo ""
if [ $drbdinstalled -lt 3 ]
then
	echo -n "DRBD Kernel module      : "
	[ $drbdinstalled -eq 1 ] && echo "OK"
	[ $drbdinstalled -eq 2 ] && echo "OK [User space tools only]"
	[ $drbdinstalled -eq 0 ] && echo "FAILED"
fi

echo -n "Machine reset (dusp)    : "
[ $dusp -eq 1 ] && echo "OK" || { echo "FAILED"; r=1; }
echo -n "MII fallback binary     : "
[ $mii_tools -eq 1 ] && echo "OK" || { echo "FAILED [Warning only]"; }
echo -n "Send ARP fallback binary: "
[ $send_arp -eq 1 ] && echo "OK" || { echo "FAILED"; r=1; }

# New for 1.2.4 - copy the logrotate config file if a /etc/logrotate.d 
# directory exists...

if [ -d /etc/logrotate.d ]
then
	cp -f /sbin/cluster/useful/logrotate.cfg /etc/logrotate.d/linuxha
	chmod 644 /etc/logrotate.d/linuxha
fi

for i in $perls_to_install
do
	ok=`/sbin/cluster/tools/install_test | egrep ^$i`
	ii=`echo "$i                             " | awk '{print substr($0,1,23)}'`
	echo -n "$ii : "
	[ "$ok" = "" ] && echo "OK" || { echo "FAILED"; r=1; }
done
if [ $r -gt 0 ]
then
	echo "Failures or warnings detected during post installation!" >/dev/tty
	echo "Please review install logs in home directory." >/dev/tty
fi
exit $r
